﻿using PlutoStudio.Dependency;
using PlutoStudio.Runtime.Session;
using System;
using System.Collections.Generic;
using System.Text;

namespace PlutoStudio.Web.Sessions
{
    public class SessionScriptManager : ISessionScriptManager, ITransientDependency
    {
        public ISession Session { get; set; }

        public SessionScriptManager()
        {
            Session = NullSession.Instance;
        }

        public string GetScript()
        {
            var script = new StringBuilder();

            script.AppendLine("(function(){");
            script.AppendLine();

            script.AppendLine("    plutostudio.session = plutostudio.session || {};");
            script.AppendLine("    plutostudio.session.userId = " + (Session.UserId.HasValue ? Session.UserId.Value.ToString() : "null") + ";");
            script.AppendLine("    plutostudio.session.impersonatorUserId = " + (Session.ImpersonatorUserId.HasValue ? Session.ImpersonatorUserId.Value.ToString() : "null") + ";");

            script.AppendLine();
            script.Append("})();");

            return script.ToString();
        }
    }
}
